package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysProcessBo;
import com.xmtlxx.system.domain.vo.SysProcessVo;
import com.xmtlxx.system.service.ISysProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 业务流程Controller
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Validated
@Api(value = "业务流程控制器", tags = {"业务流程管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysProcess")
public class SysProcessController extends BaseController {

    private final ISysProcessService iSysProcessService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysProcessVo> initBean(){
        SysProcessVo vo = new SysProcessVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询业务流程列表
     */
    @ApiOperation("查询业务流程列表")
    @SaCheckPermission("datas:query:process")
    @GetMapping("/list")
    public TableDataInfo<SysProcessVo> list(@Validated(QueryGroup.class) SysProcessBo bo, PageQuery pageQuery) {
        return iSysProcessService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业务流程列表
     */
    @ApiOperation("导出业务流程列表")
    @SaCheckPermission("system:sysProcess:export")
    @Log(title = "业务流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysProcessBo bo, HttpServletResponse response) {
        List<SysProcessVo> list = iSysProcessService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务流程", SysProcessVo.class, response);
    }

    /**
     * 获取业务流程详细信息
     */
    @ApiOperation("获取业务流程详细信息")
//    @SaCheckPermission("system:sysProcess:query")
    @GetMapping("/{pid}")
    public R<SysProcessVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("pid") String pid) {
        return R.ok(iSysProcessService.queryById(pid));
    }

    /**
     * 新增业务流程
     */
    @ApiOperation("新增业务流程")
    @SaCheckPermission("system:sysProcess:add")
    @Log(title = "业务流程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysProcessVo vo) {
        SysProcessBo bo = new SysProcessBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysProcessService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改业务流程
     */
    @ApiOperation("修改业务流程")
    @SaCheckPermission("system:sysProcess:edit")
    @Log(title = "业务流程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysProcessVo vo) {
        SysProcessBo bo = new SysProcessBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysProcessService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除业务流程
     */
    @ApiOperation("删除业务流程")
    @SaCheckPermission("system:sysProcess:remove")
    @Log(title = "业务流程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] pids) {
        return toAjax(iSysProcessService.deleteWithValidByIds(Arrays.asList(pids), true) ? 1 : 0);
    }
}
